using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ClassLibrary1;
namespace WindowsApplication1
{
    public partial class CustomerDetail : Form
    {
        public string CustomerId = "0";
        Database db;
        public CustomerDetail()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string commsql = "";
            if (CustomerId != "0")
            {
                commsql = "update Customers set Number='{1}',[Name]='{2}',Address='{3}',PhoneNumber='{4}',FaxNumber='{5}' where CustomerId={0}";
                commsql = string.Format(commsql, CustomerId, textBoxNumber.Text, textBoxName.Text, textBoxAdd.Text, textBoxTel.Text,textBoxFax.Text);
                //MessageBox.Show(commsql);
                db.ExecuteNonQuery(CommandType.Text, commsql);
                this.Close();
            }
            else
            {
                commsql = "insert into Customers (Number, [Name], Address, PhoneNumber,FaxNumber) values ('{0}','{1}','{2}', '{3}', '{4}')";
                commsql = string.Format(commsql, textBoxNumber.Text, textBoxName.Text, textBoxAdd.Text, textBoxTel.Text, textBoxFax.Text);
                db.ExecuteNonQuery(CommandType.Text, commsql);
                this.Close();
            }
        }

        private void CustomerDetail_Load(object sender, EventArgs e)
        {
            db = DatabaseFactory.CreateDatabase("Connection0");
            if (CustomerId != "0")
            {
                using (IDataReader dr = db.ExecuteReader(CommandType.Text, string.Format("SELECT [CustomerId],[Number],[Name],[Address],[PhoneNumber],[FaxNumber] FROM [dataDemo].[dbo].[Customers] where CustomerId={0}", CustomerId)))
                {
                    if (dr.Read())
                    {
                        textBoxName.Text = dr[2].ToString();
                        textBoxNumber.Text = dr[1].ToString();
                        textBoxTel.Text = dr[4].ToString();
                        textBoxAdd.Text = dr[3].ToString();
                        textBoxFax.Text = dr[5].ToString();
                    }
                }
            }

            Rectangle srec = Screen.GetBounds(this);
            this.DesktopLocation = new Point((srec.Width - this.Width) / 2, (srec.Height - this.Height) / 2);
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}